package io.jft.doll.model.viewModel

import android.arch.lifecycle.MutableLiveData
import com.g.base.extend.switchMap
import com.g.base.room.repository.NetworkBoundResult
import com.g.base.viewModel.BaseViewModel
import io.jft.doll.room.entity.remote.CartREntity
import io.jft.doll.room.repository.CartRepository

/**
 * Created by G on 2017/12/11 0011.
 */
class CartViewModel : BaseViewModel() {
    private val cartRepository by lazy { CartRepository() }
    private val operateCartList by lazy { MutableLiveData<Boolean>() }

    /**
     * 选中or反选一个商品
     * 只触发一次
     */
    fun selectProductToCart(id: String, checked: Boolean): NetworkBoundResult<CartREntity?> =
            cartRepository.checkOne(id, checked)

    /**
     * 选中or反选全部商品
     * 只触发一次
     */
    fun selectAllToCart(checked: Boolean) = cartRepository.checkAll(checked)

    /**
     * 获取商品列表
     */
    fun obsProductList() = operateCartList.switchMap {
        cartRepository.getCartList()
    }

    /**
     * 主动刷新商品列表
     */
    fun operateCartList() {
        operateCartList.value = true
    }

    fun applyShipping(addressId: String) = cartRepository.applyShipping(addressId)
}